<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class HomeListController extends Controller
{
    /*
        显示前台商品列表
    */
    public function getGoodslist(Request $request)
    {
    	//获取三级分类
        // dd($request->input('goods_name'));
        $menu = self::menu(0);

        //查询所有三级分类
        $cate = DB::table('cate')->get();
        //遍历
        $data = [];
        foreach($cate as $k=>$v){
            if(count(explode(',',$v->path)) == 3){
                $data[$cate[$k]->id] = $cate[$k]->name;
            }
        }

        //查询所有店铺
        $market = DB::table('market')->get();

        //每页显示几条
        $num = 20;
        //查询所有商品
        //判断用户是否需要查询
        if(!empty($request->input('cate'))){
            //根据分类查询
            $goods = DB::table('goods')->where('cate_id',$request->input('cate'))->paginate($num);
        }else if(!empty($request->input('size'))){
            //根据大小查询
            $goods = DB::table('goods')->where('goods_size','like','%'.$request->input('size').'%')->paginate($num);
        }else if(!empty($request->input('color'))){
            //根据颜色查询
            $goods = DB::table('goods')->where('goods_color','like','%'.$request->input('color').'%')->paginate($num);
        }else if(!empty($request->input('min')) && !empty($request->input('max'))){
            //接值
            $min = $request->input('min');
            $max = $request->input('max');
            //判断是否为数字
            if(is_numeric($min) && is_numeric($max)){
                //根据价格查询
                $goods = DB::table('goods')->whereBetween('goods_price',[$min,$max])->paginate($num);
            }else{
                $goods = DB::table('goods')->paginate($num);
            }
        }else if(!empty($request->input('goods_name'))){
            $goods = DB::table('goods')->where('goods_name','like','%'.$request->input('goods_name').'%')->paginate($num);
        }else if(!empty($request->input('market'))){
            //根据店铺查询
            $goods = DB::table('goods')->where('market_id',$request->input('market'))->paginate($num);
        }else{
            $goods = DB::table('goods')->paginate($num);
        }

        //解析模板
        return view('home.list.list',['goods'=>$goods,'market'=>$market,'menu'=>$menu,'cate'=>$data,'request'=>$request->all()]);
    }



    // 购物车
    public function getCart()
    {
    	// 判断是否有session值
	    	// 接收session的值
	    	$goods = session('cart');
            // dd($goods);
    	if (!empty($goods)){

	    	// 自定义的session值
	    	// $goods = [['id'=>1,'num'=>3],['id'=>2,'num'=>5]];

	    	// 打印接收的session值
	    	// dd($goods);
	    	$data = [];
	    	foreach ($goods as $k => $v) {
	    		$tem = DB::table('goods')->where('id',$v['id'])->first();
	    		$tem->num = $v['num'];
                $tem->color = $v['color'];
                $tem->size = $v['size'];
	    		$data[] = $tem;
	    	}
	    	// dd($data);
            
	    	$tsum = 0;
	    	foreach ($data as $k => $v) {
	    		$tsum += $v->num * $v->goods_price;
	    	}
             $menu = self::menu(0);
	    	// dd($tsum);
	    		
	    	return view('home.cart.cart',['goods'=>$data,'tsum'=>$tsum,'menu'=>$menu]);
	    }else{
            $menu = self::menu(0);
	    	return view('home.cart.empty',['menu'=>$menu]);
	    }
    }

    // 确认订单页面
     public function postRecart(Request $request)
    {     
        if(!session('id')){
            return redirect('/login');
        }
        $goods = $request->only(['goods_id','goods_num','goods_pic','goods_name','goods_price','color','size','goods_num']);
        // dd($goods);
    	// 接收的值
        // 商品id
         $menu = self::menu(0);

        $tem = [];
         foreach ($goods as $k => $v) {
           foreach($v as $kk=>$vv){
                $tem[$kk][$k] = $v[$kk];
           }
        
        }
       // dd($tem);
        
        // dd($size);
        // dd($goods_id);
    	
    	// dd($goods_id);
    	$goods_total = $request->input('goods_total');

    	// dd($goods_total);
    	// 判断是否有session值
    	// 接收session的值
	

    	
    	// session(['id'=>32]);
   		 // session(['id'=>32]);
    	$ause_id = session('id');
    	// $ause_id = 32;

    	$address = DB::table('addr')->where('ause_id',$ause_id)->get();
    	// dd($address);

    	// dd($tsum);
    		
    	return view('home.cart.recart',['goods'=>$tem,'tsum'=>$goods_total,'address'=>$address,'menu'=>$menu]);
	   
    }

    /*
        商品详情页显示
    */
    public function getDetail(Request $request)
    {
        //获取三级分类
        $menu = self::menu(0);

        //获取id
        $id = $request->input('id');

        //根据id查询该商品的详细信息
        $goods = DB::table('goods')
                    ->join('market','goods.market_id','=','market.id')
                    ->select('goods.*','market.mar_name','market.mar_pic')
                    ->where('goods.id',$id)
                    ->first();

        //把该商品的color转化为数组
        $goods->goods_color = explode(',',$goods->goods_color);
        //把该商品的尺码转化为数组
        $goods->goods_size = explode(',',$goods->goods_size);

        //好评 或 差评 默认查询好评
        $status = $request->input('status',1);

        //查询当前商品所有评论条数
        $num = count(DB::table('comment')->where('goods_id',$id)->get());

        //根据商品查询同类
        $good = DB::table('goods')->where('cate_id',$goods->cate_id)->where('id','<>',$id)->paginate(16);

        //根据商品查询本店商品
        $market_goods = DB::table('goods')->where('market_id',$goods->market_id)->where('id','<>',$goods->id)->paginate(5);

        //根据商品id查询评论
        $comment = DB::table('comment')
                    ->join('user','user.id','=','comment.user_id')
                    ->select('comment.*','user.username','user.pic')
                    ->where('comment.goods_id',$id)
                    ->where('comment.status',$status)
                    ->paginate(10);

        //查询词语过滤表
        $data = DB::table('sensitive')->get();
        //定义两个空数组 用来接收 需要替换的词 和 替换的词
        $keyword = [];
        $replace = [];
        //遍历
        foreach($data as $k=>$v){
            $keyword[] = $v->keyword;
            $replace[] = $v->replace;
        }
        //遍历替换评论中的敏感词
        foreach($comment as $k=>$v){
            $comment[$k]->content = str_replace($keyword,$replace,$v->content);
        }

        //解析模板
        return view('home.detail.detail',[
                'goods'=>$goods,
                'good'=>$good,
                'menu'=>$menu,
                'comment'=>$comment,
                'num'=>$num,
                'market_goods'=>$market_goods
        ]);
    }

    /*
        商品评论 赞
    */
    public function getAssist(Request $request)
    {
        $id = $request->input('id');
        //查询点赞总数
        $comment = DB::table('comment')->where('id',$id)->first();
        //点赞总数加1
        $assist = $comment->assist += 1;
        $data = [];
        $data['assist'] = $assist;
        //执行修改
        $res = DB::table('comment')->where('id',$id)->update($data);
        if($res){
            echo 1;
        }else{
            echo 2;
        }
    }

    /*
        商品评论 踩
    */
    public function getStamp(Request $request)
    {
        $id = $request->input('id');
        //查询点赞总数
        $comment = DB::table('comment')->where('id',$id)->first();
        //点赞总数加1
        $stamp = $comment->stamp += 1;
        $data = [];
        $data['stamp'] = $stamp;
        //执行修改
        $res = DB::table('comment')->where('id',$id)->update($data);
        if($res){
            echo 1;
        }else{
            echo 2;
        }
    }

    /*
        添加商品到购物车
    */
    public function getInsert(Request $request)
    {
        //提取部分数据
        $data = $request->only(['id','num','color','size']);
        //判断购物车是否有商品
        if($this->check($data['id'])){
            //获取购物车中的值
            $cart = session('cart');
            //遍历判断
            foreach($cart as $k=>$v){
                if($v['id'] == $data['id']){
                    $cart[$k]['num'] += $data['num'];
                }
            }
            //将更新后的购物车重新添加至session
            session(['cart'=>$cart]);
        }else{
            $request->session()->push('cart',$data);
        }
        echo 1;
    }

    /*
        判断购物车是否有当前商品
    */
    private function check($id)
    {
        //获取购物车的值
        $cart = session('cart');
        if(empty($cart)){
            return false;
        }
        //遍历判断
        foreach($cart as $k=>$v){
            if($v['id'] == $id){
                //商品存在
                return true;
            }
        }
        //商品不存在
        return false;
    }

    public function getDcart(Request $request)
    {

    	$cart = session('cart');
    	$id = $request->input('id');

    	// dd($id);
    	// 执行删除
    	unset($cart[$id]);
    	// dd($res);
		// 更新后的session值
		session(['cart'=>$cart]);
		echo 1;
    }

    /*
        三级菜单
    */
    public static function menu($pid)
    {
        //查询所有pid为0的顶级分类
        $res = DB::table('cate')->where('pid',$pid)->get();

        $data = [];
        //开始遍历
        foreach($res as $k=>$v){
            //开始递归
            $v->sub = self::menu($v->id);
            $data[] = $v;
        }
        //返回
        return $data;
    }


}  
